<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>物流编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" th:href="@{/css/font.css}">
    <link rel="stylesheet" th:href="@{/css/xadmin.css}">
    <link rel="stylesheet" th:href="@{/fonts/iconfont/iconfont.css}">
    <script type="text/javascript" th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/js/xadmin.js}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
        <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
        <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form" name="list-add" action="" method="post" id="list-add">
              <div class="layui-form-item">
                  <label for="lname" class="layui-form-label">
                      <span class="x-red">*</span>物流名称
                  </label>
                  <div class="layui-input-inline">
                      <input hidden id="lid" name="lid" th:value="${lid}" required>
                      <input type="text" id="lname" name="lname" required="required" lay-verify="required"
                      autocomplete="off" class="layui-input">
                  </div>
              </div>

              <div class="layui-form-item">
                    <label for="lname" class="layui-form-label">
                        <span class="x-red">*</span>是否启用
                    </label>
                    <div class="layui-input-inline">
                        <input type="checkbox" name="state" id="state" checked="checked" lay-skin="switch" lay-text="启用|停用">
                    </div>
              </div>

                <div class="layui-form-item">
                    <label for="sort" class="layui-form-label">
                        <span class="x-red">*</span>排序优先值
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="sort" name="sort" required="required" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>


              <div class="layui-form-item">
                  <button  class="layui-btn" lay-filter="add" lay-submit="" type="submit">
                      添加
                  </button>
              </div>
          </form>
        </div>
    </div>
    <script type="text/javascript" th:src="@{/lib/rep/jquery-2.1.1/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/lib/jquery.form/jquery-form.js}"></script>
    <script type="text/javascript" th:src="@{/lib/jquery.validation/1.14.0/validate-methods.js}"></script>
    <script type="text/javascript" th:src="@{/lib/jquery.validation/1.14.0/jquery.validate.js}"></script>
    <script type="text/javascript" th:src="@{/lib/jquery.validation/1.14.0/messages_zh.js}"></script>
    <script type="text/javascript" th:src="@{/js/commons.js}"></script>
    <script>
        $(function () {

        });
        let layer;
        let form;
        layui.use(['form', 'layer'],
        function() {
            $ = layui.jquery;
            form = layui.form;
            layer = layui.layer;
            //同步获取物流信息并渲染
            let id = $('#lid').val();
            $.ajax({
                async: false,
                url: '/logistics/info',
                type: 'post',
                data: {lid:id},
                success: function (result) {
                    let r_icon = icon(result);
                    if (result.type === 'success'){
                        set_data(result.data);
                    }else {
                        layer.msg(result.content,{icon:r_icon});
                    }
                },
                error:function(XMLHttpRequest) {
                    layer.alert('数据处理失败! 错误码:'+XMLHttpRequest.status,{title: '错误信息',icon: 2});
                }
            });
            form.render('checkbox');
        });
        //设置数据
        function set_data(obj) {
            $('#lname').val(obj.logisticsName);
            $('#sort').val(obj.sort);
            if (obj.state === 0){
                $('#state').removeAttr('checked');
            }
        }

        //保存提交
        $("#list-add").validate({
            rules:{
                lname:{
                    required:true,
                    maxlength:20
                },
                sort:{
                    required:true,
                    digits:true,
                    maxlength:4
                }
            },
            onkeyup:false,
            focusCleanup:false,
            success:"valid",
            submitHandler:function(form){
                let index = layer.load(3);
                $(form).ajaxSubmit({
                    url: "/logistics/edit",//接口路径
                    type: "post",
                    success: function(data) {
                        let r_icon = icon(data);
                        if (data.type === 'success'){
                            layer.confirm(data.content, {
                                btn: ['确认'],icon: r_icon
                            }, function(){
                                let index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                table = parent.layui.table;
                                table.reload('listTest');
                            });
                        }else {
                            layer.msg(data.content,{icon:r_icon});
                            layer.closeAll('loading');
                        }
                    },
                    error:function(XMLHttpRequest) {
                        layer.close(index);
                        layer.alert('数据处理失败! 错误码:'+XMLHttpRequest.status,{title: '错误信息',icon: 2});
                    }
                });
            }
        });
    </script>
</body>

</html>
